package cmpe273.classproject.server.service;

import java.sql.SQLException;
import java.util.List;

import cmpe273.classproject.server.dao.DaoConst;
import cmpe273.classproject.server.dao.VideoRentalDao;
import cmpe273.classproject.server.dto.MovieResponse;
import cmpe273.classproject.server.dto.PersonResponse;
import cmpe273.classproject.server.dto.TransactionResponse;
import cmpe273.classproject.server.model.Movie;
import cmpe273.classproject.server.model.Person;
import cmpe273.classproject.server.model.RentalTransaction;

public class MovieServiceImpl implements MovieService {

	private VideoRentalDao dao;
	
	public MovieServiceImpl() {
		dao = new VideoRentalDao();
	}
	
	//Test service
	/**
	 * @param args
	 */
	public static void main(String... args) {
		System.out.println("Running movie service");
		
		Person person = new Person();
		
		person.setMemberId(14);
		person.setType(1);
		person.setFirstName("updated pf");
		person.setCity("u fremont");
		person.setEmailId("u p@p.p");
		person.setAddress("123");
		person.setLastName("u pl");
		person.setState("ga");
		person.setZipCode("312");
		
		MovieServiceImpl service = new MovieServiceImpl();
		PersonResponse response = service.deletePerson(14);
		
		//PersonResponse response = service.createPerson(person);
		//PersonResponse response = service.deletePerson(1236);
		//PersonResponse response = service.getAllPersons();
		//Person[] response = service.getAllSimpleCustomers();
		//PersonResponse response = service.findPerson("parag", "", "", "", "", "", "p@p.p", "");
		//Person[] result = response.getPersons();
		//System.out.println(response[0].getFirstName());
		//PersonResponse response = service.getAllPremiumMembers();
		//PersonResponse response = service.updatePerson(person);
		
		/*
		Movie m = new Movie();
		m.setMovieId(12);
		m.setMovieName("updated");
		m.setAvailableCopy(-1);
		m.setRentAmount(-1);
		m.setTotalCopies(-1);
		m.setMovieCategory("updated");
		MovieResponse response = service.updateMovie(m);
		*/
//		List<Movie> movieList = new ArrayList<Movie>(5000);
//		for(int i = 0; i < 1000; i++) {
//			Movie movie = new Movie();
//			movie.setMovieBanner("There are no clean getaways");
//			movie.setMovieCategory(1);
//			movie.setMovieName("No country for old men " + i);
//			movie.setRentAmount(15);
//			movie.setTotalCopies(1000);
//			movie.setReleaseDate(new Date());
//			movieList.add(movie);
//		}
//			
//		for(Movie m : movieList) {
////			System.out.println(service.createMovie(m).getErrorMessage());
//			service.createMovie(m).getErrorMessage();
//		}
//		
//		Movie m = new Movie();
//		m.setMovieId(15);
//		PersonResponse pr = service.getAllPersonsByMovie(m);
//		if(pr.isError() == 0) {
//			List<Person> persons = new ArrayList<Person>(Arrays.asList(pr.getPersons()));
//			for(Person mm : persons) {
//				System.out.println(mm.getFirstName());
//			}
//		}
//		else {
//			System.out.println("Error: " + pr.getErrorMessage());
//		}
		
		//Movies by category
//		MovieResponse mr = service.getAllMoviesInCategory(1);
//		if(mr.isError() == 0) {
//			List<Movie> movies = new ArrayList<Movie>(Arrays.asList(mr.getMovies()));
//			for(Movie m : movies) {
//				System.out.println(m.getMovieName());
//			}
//		}
//		else {
//			System.out.println("Error: " + mr.getErrorMessage());
//		}
//		
//		//All movies in db
//		MovieResponse mr2 = service.getAllMovies();
//		if(mr.isError() == 0) {
//			List<Movie> movies = new ArrayList<Movie>(Arrays.asList(mr2.getMovies()));
//			for(Movie m : movies) {
//				System.out.println(m.getMovieName());
//			}
//		}
//		else {
//			System.out.println("Error: " + mr2.getErrorMessage());
//		}
	}
	
	
	@Override
	public int createPerson(Person customer) {
		PersonResponse r = new PersonResponse();
		r.setError(0);
		int result = 0;
		try {
			if(dao.createPerson(customer) == DaoConst.DUPLICATE) {
				r.setError(-1);
				r.setErrorMessage("Member already exists.");
				result = -1;
			}				
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
			result = -1;
		}
		return result;
	}

	@Override
	public PersonResponse deletePerson(long id) {
		PersonResponse r = new PersonResponse();
		r.setError(0);
		try {
			Boolean bResult = dao.deletePerson(id);
			if (!bResult)
			{
				r.setError(-1);
				r.setErrorMessage("Error!");
			}
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public MovieResponse createMovie(Movie movie) {
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			int result = dao.createMovie(movie);
			System.out.println(result);
			if(result == DaoConst.DUPLICATE) {
				r.setError(-1);
				r.setErrorMessage("Movie already exists.");
			}		
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public MovieResponse deleteMovie(Movie movie) {
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			int result = dao.deleteMovie(movie.getMovieId());
			if(result < 1) {
				r.setError(-1);
				switch(result) {
				case DaoConst.NOT_FOUND: r.setErrorMessage("Cannot delete movie. Movie with id "+ movie.getMovieId() + " not found."); break;
				default: r.setErrorMessage("Delete movie failed."); break;
				}
			}
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public MovieResponse issueMovie(Movie movie, Person person) {
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			if(movie.getMovieId() > 0 && person.getMemberId() > 0) {
				int result = dao.issueMovie(movie, person);
				if(result == DaoConst.ALREADY_ISSUED) {
					r.setError(-1);
					r.setErrorMessage("Member already issued this movie.");
				}
				if(result == DaoConst.EXCEEDED_LIMIT) {
					r.setError(-1);
					r.setErrorMessage("Member has exceeded movie rent limit.");
				}
				if(result == DaoConst.MOVIE_UNAVAILABLE) {
					r.setError(-1);
					r.setErrorMessage("No more copies of this movie are available.");
				}
			}
			else {
				r.setError(-1);
				r.setErrorMessage("Invalid paramaters.");
			}
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public MovieResponse submitMovie(RentalTransaction rt) {
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			
				dao.submitMovie(rt);
			
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public PersonResponse getPerson(int personId) {
		PersonResponse r = new PersonResponse();
		r.setError(0);
		try {
			Person[] person = new Person[1];
			person[0] = dao.readPerson(personId);
			r.setPersons(person);
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}
	
	@Override
	public Person[] getAllPremiumMembers() {
		PersonResponse r = new PersonResponse();
		Person[] arpersons = null;
		List<Person> persons = null;
		r.setError(0);
		try {
			persons = dao.getAllPremiumMembers();
			arpersons = new Person[persons.size()];
			
			for (int i = 0; i< persons.size(); i++)
			{
				arpersons[i] = persons.get(i);
			}	
			
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return arpersons;
	}
/*
	@Override
	public PersonResponse getAllSimpleCustomers() {
		PersonResponse r = new PersonResponse();
		r.setError(0);
		try {
			r.setPersons(dao.getAllSimpleCustomers().toArray(new Person[0]));
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}
*/	
	
	@Override
	public Person[] getAllSimpleCustomers() {
		PersonResponse r = new PersonResponse();
		Person[] arpersons = null;
		List<Person> persons = null;
		r.setError(0);
		try {
			persons = dao.getAllSimpleCustomers();
			arpersons = new Person[persons.size()];
			
			for (int i = 0; i< persons.size(); i++)
			{
				arpersons[i] = persons.get(i);
			}						
			
			r.setPersons(arpersons);
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return arpersons;
	}

	@Override
	public MovieResponse getAllMovies() {
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			r.setMovies(dao.getAllMovies().toArray(new Movie[0]));
		} catch (SQLException e) {
			r.setError(1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public MovieResponse getAllMoviesByPerson(int memberID){
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			r.setMovies(dao.getAllMoviesByPerson(memberID).toArray(new Movie[0]));
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}
	
	@Override
	public MovieResponse getAllOutstandingMoviesByPerson(int memberID) {
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			r.setMovies(dao.getAllMoviesByPerson(memberID).toArray(new Movie[0]));
		} catch (SQLException e) {
			r.setError(1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}
	
	@Override
	public MovieResponse getAllMoviesInCategory(String category) {
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			r.setMovies(dao.getMoviesByCategory(category).toArray(new Movie[0]));
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}
	
	@Override
	public PersonResponse getAllPersons() {
		PersonResponse r = new PersonResponse();
		r.setError(0);
		try {
			r.setPersons(dao.getAllPersons().toArray(new Person[0]));
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public PersonResponse updatePerson(Person person) {
		PersonResponse r = new PersonResponse();
		r.setError(0);
		try {
			Boolean bResult = dao.updatePerson(person);
			if (!bResult)
			{
				r.setError(-1);
				r.setErrorMessage("Error!");
			}
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public MovieResponse updateMovie(Movie movie) {
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			dao.updateMovie(movie);
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public PersonResponse findPerson(String firstName, String lastName, String address, String city, String state, String zipcode, String emailaddress, String phoneno) {
		PersonResponse r = new PersonResponse();
		r.setError(0);
		try {
			r.setPersons(dao.findPerson(firstName, lastName, address, city, state, zipcode, emailaddress, phoneno).toArray(new Person[0]));
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public MovieResponse findMovie(Movie movie) {
		System.out.println("findmovie-");
		MovieResponse r = new MovieResponse();
		r.setError(0);
		try {
			System.out.println("findmovie--");
			r.setMovies(dao.findMovie(movie).toArray(new Movie[0]));
		} catch (SQLException e) {
			e.printStackTrace();
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public PersonResponse getAllPersonsByMovie(Movie movie) {
		PersonResponse r = new PersonResponse();
		r.setError(0);
		try {
			r.setPersons(dao.getAllPersonsByMovie(movie.getMovieId()).toArray(new Person[0]));
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public TransactionResponse getHistory(int memberId) {
		TransactionResponse r = new TransactionResponse();
		r.setError(0);
		try {
			r.setTransactions(dao.getHistory(memberId).toArray(new RentalTransaction[0]));
		} catch (SQLException e) {
			r.setError(-1);
			r.setErrorMessage(e.getMessage());
		}
		return r;
	}

	@Override
	public boolean login(String username, String password) {
		try {
			return dao.login(username, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
